package indi.yangshenhui.tool;

import java.util.Collection;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public class CheckUtil {

	private CheckUtil() {
	}

	public static void checkNull(Object object) {
		if (object == null) {
			throw new RuntimeException("object is null");
		}
	}

	public static void checkEmptyString(String string) {
		if (StringUtil.isEmpty(string)) {
			throw new RuntimeException("string is null or empty");
		}
	}

	public static void checkEmptyMap(Map<?, ?> map) {
		if (CollectionUtil.isEmpty(map)) {
			throw new RuntimeException("map is null or empty");
		}
	}

	public static void checkEmptyCollection(Collection<?> collection) {
		if (CollectionUtil.isEmpty(collection)) {
			throw new RuntimeException("collection is null or empty");
		}
	}

	public static void checkEmptyArray(Object[] objects) {
		if (ArrayUtil.isEmpty(objects)) {
			throw new RuntimeException("objects is null or empty");
		}
	}

	public static void checkZero(int i) {
		if (i <= 0) {
			throw new RuntimeException(i + " less than zero");
		}
	}

	public static void checkZero(long l) {
		if (l <= 0L) {
			throw new RuntimeException(l + " less than zero");
		}
	}

	public static final String NUMBER = "[0-9]*";
	public static final String PHONE = "^1\\d{10}$";
	// 验证由26个英文字母组成的字符串：^[A-Za-z]+$
	public static final String A_Za_z = "[A-Za-z]+";
	// 验证由26个大写英文字母组成的字符串：^[A-Z]+$
	public static final String A_Z = "[A-Z]+";
	// 验证由26个小写英文字母组成的字符串：^[a-z]+$
	public static final String a_z = "[a-z]+";
	// 验证由数字和26个英文字母组成的字符串：^[A-Za-z0-9]+$
	public static final String A_Za_z0_9 = "[A-Za-z0-9]+";
	public static final String EMAIL = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";

	/**
	 * 
	 * matcher根据传入的正则表达式判断传入的字符串是否符合
	 * 
	 * @param regex
	 * @param content
	 * @return boolean
	 * @exception @since
	 *                1.0.0
	 */
	public static boolean matcher(String regex, String content) {
		Matcher matcher = Pattern.compile(regex).matcher(content);
		return matcher.matches();
	}

	/**
	 * 过滤regex正则表达式中的字符 stringFilter
	 * 
	 * @param regex
	 *            正则表达式
	 * @param content
	 *            源字符
	 * @return String 过滤的字符
	 * @exception @since
	 *                1.0.0
	 */
	public static String stringFilter(String regex, String content) {
		if (StringUtils.isBlank(content)) {
			return "";
		}
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(content);
		return m.replaceAll("").trim();
	}

}